package cn.xy.base.server.configure;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.xy.base.server.config.RedissonConfig;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.redisson.config.TransportMode;
import org.redisson.spring.data.connection.RedissonConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.io.IOException;

/**
 * redisson配置
 *
 *  @author xy
 */
@Configuration
@Slf4j
public class RedissonConfiguration {

    @Autowired
    private RedissonConfig redissonConfig;

    @Bean
    public RedissonConnectionFactory redissonConnectionFactory(RedissonClient redisson) {
        return new RedissonConnectionFactory(redisson);
    }

    @Bean("redissonClient")
    public RedissonClient redissonClient() throws IOException {
        Config config = new Config();
        config.setTransportMode(TransportMode.NIO);
        config.setThreads(redissonConfig.getThreads());
        config.setNettyThreads(redissonConfig.getNettyThreads());

        SingleServerConfig singleServerConfig = config.useSingleServer();
        singleServerConfig.setAddress(redissonConfig.getAddress());
        if (CharSequenceUtil.isNotEmpty(redissonConfig.getUsername())) {
            singleServerConfig.setUsername(redissonConfig.getUsername());
        }
        if (CharSequenceUtil.isNotEmpty(redissonConfig.getPassword())) {
            singleServerConfig.setPassword(redissonConfig.getPassword());
        }
        singleServerConfig.setDatabase(redissonConfig.getDatabase());
        singleServerConfig.setClientName(redissonConfig.getClientName());

        if (redissonConfig.isSslEnabled()) {
            singleServerConfig.setSslEnableEndpointIdentification(redissonConfig.isSslEnableEndpointIdentification());

            if (CharSequenceUtil.isNotEmpty(redissonConfig.getSslKeystore())) {
                File sslKeyStoreFile = FileUtil.file(redissonConfig.getSslKeystore());
                singleServerConfig.setSslKeystore(sslKeyStoreFile.toURI().toURL());
                singleServerConfig.setSslKeystorePassword(redissonConfig.getSslKeystorePassword());
            }
            log.info("sslTrustStore=> {}", redissonConfig.getSslTruststore());
            File sslTrustStoreFile = FileUtil.file(redissonConfig.getSslTruststore());
            log.info("sslTrustStoreFile=> {}", sslTrustStoreFile);
            singleServerConfig.setSslTruststore(sslTrustStoreFile.toURI().toURL());
            singleServerConfig.setSslTruststorePassword(redissonConfig.getSslTruststorePassword());
        }

        singleServerConfig.setIdleConnectionTimeout(redissonConfig.getIdleConnectionTimeout());
        singleServerConfig.setConnectTimeout(redissonConfig.getConnectTimeout());
        singleServerConfig.setTimeout(redissonConfig.getTimeout());
        singleServerConfig.setRetryAttempts(redissonConfig.getRetryAttempts());
        singleServerConfig.setRetryInterval(redissonConfig.getRetryInterval());

        singleServerConfig.setSubscriptionsPerConnection(redissonConfig.getSubscriptionsPerConnection());
        singleServerConfig.setSubscriptionConnectionMinimumIdleSize(redissonConfig.getSubscriptionConnectionMinimumIdleSize());
        singleServerConfig.setSubscriptionConnectionPoolSize(redissonConfig.getSubscriptionConnectionPoolSize());
        singleServerConfig.setConnectionMinimumIdleSize(redissonConfig.getConnectionMinimumIdleSize());
        singleServerConfig.setConnectionPoolSize(redissonConfig.getConnectionPoolSize());
        singleServerConfig.setDnsMonitoringInterval(redissonConfig.getDnsMonitoringInterval());

        return Redisson.create(config);
    }
}
